<script setup>
import {getCurrentInstance, h, onMounted, ref} from "vue";
import ProcessRelationConfig from "@/views/process/ProcessRelationConfig.vue";
import ProcessRun from "@/views/process/ProcessRun.vue";
import jobApi from '@/api/job/jobApi'
import Group from '@/components/Group/index.vue'
import {message} from 'ant-design-vue';

const {proxy} = getCurrentInstance();
const refresh = () => {
	if (props.hiddenFlag) {
		return
	}
	tableRef.value.refresh();
}
const search = ref()

const props = defineProps(["hiddenFlag", "productId", "operaCallback", "id"])
const tableRef = ref();
const productId = ref()
const columns = ref([
	{
		title: '名称',
		key: 'jobName',
		dataIndex: 'jobName',
		ellipsis: true,
	},
	{
		title: '编码',
		key: 'jobCode',
		dataIndex: 'jobCode',
		ellipsis: true,
	},
	{
		title: '描述',
		key: 'jobDescription',
		dataIndex: 'jobDescription',
		ellipsis: true,
	},
	{
		title: 'cron',
		key: 'cron',
		dataIndex: 'cron',
		ellipsis: true,
	},
	{
		title: 'state',
		key: 'state',
		dataIndex: 'state',
		mapping: [{
			key: '1',
			value: '运行'
		},
			{
				key: '2',
				value: '暂停'
			}],
		ellipsis: true,
	},
	{
		title: '修改日期',
		key: 'modifyTime',
		dataIndex: 'modifyTime',
		ellipsis: true,
	},
	{
		title: '操作',
		key: 'action',
		dataIndex: 'action',
		ellipsis: true,
		width: 730,
		fixed: 'right',
		buttonList: [
			{
				click: (field, record) => {
					views(record)
				},
				render: (record) => {
					return "查看";
				}
			},
			{
				type: 'primary',
				click: (field, record) => {
					edit(record)
				},
				render: (record) => {
					return "修改";
				}
			},
			{
				click: (field, record) => {
					remove(record)
				},
				render: (record) => {
					return '删除';
				}
			},
			{
				click: (field, record) => {
					runOne(record)
				},
				render: (record) => {
					return "运行一次";
				}
			},
			{

				click: (field, record) => {
					run(record)
				},
				render: (record) => {
					return "运行";
				}
			},
			{

				click: (field, record) => {
					stop(record)
				},
				render: (record) => {
					return "停止";
				}
			},
			{

				click: (field, record) => {
					nextFireTime(record)
				},
				render: (record) => {
					return "下次执行时间";
				}
			},
			{

				click: (field, record) => {
					relationProcess(record)
				},
				render: (record) => {
					return "关联流程";
				}
			}
		]
	}
])
onMounted(() => {
	productId.value = props.productId
	if (!productId.value) {
		productId.value = proxy.$route.query.id;
	}
})
const save = async () => {
	jobConfig.value.groupId = currentGroupId.value == 0 ? null : currentGroupId.value
	jobConfig.value.productId = productId.value
	if (!jobConfig.value.groupId) {
		message.error("请选择分组")
		return
	}
	if (jobConfig.value.id) {
		const s = await jobApi.updateJobConfigById(jobConfig.value);
		if (props.operaCallback) {
			props.operaCallback('edit', 'job', {
				...jobConfig.value
			})
		}
	} else {
		const s = await jobApi.jobConfigAdd(jobConfig.value);
		if (props.operaCallback) {
			props.operaCallback('add', 'job', {
				id: s,
				...jobConfig.value
			})
		}
	}
	jobConfig.value = {}
	jobConfig.value.visible = false;
	refresh();
}
const jobConfig = ref({
	visible: false
});
const queryList = async () => {
	if (props.id) {
		let job = await jobApi.getJobConfigById({
			id: props.id
		})
		return [job]
	}
	return await jobApi.queryJobConfigList({
		groupId: currentGroupId.value == 0 ? null : currentGroupId.value,
		productId: productId.value,
		id: props.id
	});

}
const processRelationVisible = ref(false);
const views = async (row) => {
	const a = await jobApi.getJobConfigById({id: row.id});
	jobConfig.value = a;
	jobConfig.value.visible = true;
}

const processRunFlag = ref(false);
const edit = async (row) => {
	const a = await jobApi.getJobConfigById({id: row.id});
	jobConfig.value = a;
	jobConfig.value.visible = true;
}

const remove = async (row) => {
	const a = await jobApi.removeById(row.id);
	refresh();
	if (props.operaCallback) {
		props.operaCallback('delete', 'job', {
			id: row.id,
			...row.value
		})
	}
}
const runOne = async (row) => {
	// const a = await api.runOneById(row.id);
	// refresh();

	processRunFlag.value = true;
	bizId.value = null;
	setTimeout(() => {
		bizId.value = row.id;
	}, 0)
}
const run = async (row) => {
	const a = await jobApi.run(row.id);
	refresh();
}
const stop = async (row) => {
	const a = await jobApi.stop(row.id);
	refresh();
}
const nextFireTime = async (row) => {
	const a = await jobApi.nextFireTime(row.id);
	proxy.$notify({
		title: '提示',
		message: h('i', {style: 'color: teal'}, a)
	});
}

const bizId = ref();
const relationProcess = (row) => {
	bizId.value = row.id;
	processRelationVisible.value = true;

}
const currentGroupId = ref(0);

const selectTree = (id, code, name) => {
	currentGroupId.value = id;
	refresh();
}


const show = (action, data) => {
	if (action == 'add') {
		currentGroupId.value = data.id;
		jobConfig.value.visible = true
	} else if (action == 'delete') {
		remove(data);
	} else if (action == 'edit') {
		currentGroupId.value = data.groupId;
		edit(data);
	}
}

defineExpose({
	show
})
</script>

<template>
	<a-row :gutter="10" class="mb-2" v-if="!props.hiddenFlag">
		<a-col :span="5" v-if="!props.id">
			<Group height="calc(71vh)" :selectTree="selectTree" relationTypeCode="SCENE" :productId="productId"></Group>
		</a-col>
		<a-col :span="props.id?24:19">
			<a-row :gutter="10" class="mb-2" v-if="!props.id">
				<a-col :span="24">
					<a-card>
						<a-space>
							<a-input-search placeholder="请输入内容" v-model:value="search"
											@search="refresh"></a-input-search>
							<a-button @click="jobConfig.visible = true;" type="primary">新增</a-button>
						</a-space>
					</a-card>
				</a-col>
			</a-row>

			<a-row :gutter="10" class="mb-2">
				<a-col :span="24">
					<a-card>
						<h-table height="calc(70vh)" :columns="columns" ref="tableRef" :data="queryList">

						</h-table>
					</a-card>
				</a-col>
			</a-row>
		</a-col>
	</a-row>

	<a-modal v-model:open="jobConfig.visible" :closable="false" width="300px">
		<a-form :model="jobConfig">
			<a-form-item label="名称">
				<a-input v-model:value="jobConfig.jobName"></a-input>
			</a-form-item>

			<a-form-item label="编码">
				<a-input v-model:value="jobConfig.jobCode"></a-input>
			</a-form-item>

			<a-form-item label="cron">
				<cron v-model="jobConfig.cron"></cron>
			</a-form-item>

			<a-form-item label="描述">
				<a-input v-model:value="jobConfig.jobDescription"></a-input>
			</a-form-item>

			<a-form-item label="参数">
				<a-input v-model:value="jobConfig.params"></a-input>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-space>
				<a-button @click="jobConfig.visible = false">取 消</a-button>
				<a-button type="primary" @click="save">确 定</a-button>
			</a-space>
		</template>
	</a-modal>

	<a-modal v-model:open="processRelationVisible" :closable="false" width="1000px">
		<ProcessRelationConfig :biz-id="bizId" biz-type="JOB"></ProcessRelationConfig>
	</a-modal>

	<a-modal v-model:open="processRunFlag" :closable="false" width="1000px">
		<ProcessRun v-if="processRunFlag" :biz-id="bizId" biz-type="JOB"></ProcessRun>
	</a-modal>

</template>

<style scoped></style>
